package com.objetdirect.gwt.umldrawer.client;

import java.util.Date;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.MouseOutEvent;
import com.google.gwt.event.dom.client.MouseOutHandler;
import com.google.gwt.event.dom.client.MouseOverEvent;
import com.google.gwt.event.dom.client.MouseOverHandler;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Tree;
import com.google.gwt.user.client.ui.TreeItem;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.datepicker.client.DatePicker;

public class AccountPageContent implements PageContent {

	private VerticalPanel mainPanel = new VerticalPanel();
	private VerticalPanel wholeBody = new VerticalPanel();
	private HorizontalPanel body = new HorizontalPanel();
	private VerticalPanel leftBody = new VerticalPanel();
	private VerticalPanel rightBody = new VerticalPanel();
	private Image profilePic = new Image();
	private Image header = new Image();
	private Image footer = new Image();
	private Label welcome = new Label();
	public Label timerLabel = new Label();
	private TreeItem individualProjects = new TreeItem();
	private TreeItem teamProjects = new TreeItem();
	private TreeItem createProject = new TreeItem();
	private TreeItem createIndividual = new TreeItem();
	private TreeItem createTeam = new TreeItem();
	private TreeItem onlineUsers = new TreeItem();
	private Tree individualTree = new Tree();
	private Tree teamTree = new Tree();
	private Tree creteTree = new Tree();
	private Tree onlineTree = new Tree();
	private TextBox individualProjectNameBox = new TextBox();
	private TextBox teamProjectNameBox = new TextBox();
	private TextArea membersNames = new TextArea();
	private DatePicker datePicker = new DatePicker();
	
	@Override
	public void setPageContent() {
		header.setUrl("images/header.png");
		header.setWidth("1201px");
		header.setStyleName("headerImage");
		
		footer.setUrl("images/footer.png");
		footer.setWidth("1201px");
		
		profilePic.setUrl("profilePics/andrew.jpg");
		profilePic.setStyleName("profilePic");
		
		datePicker.setValue(new Date(), false);
		datePicker.setStyleName("date");
		datePicker.setWidth("250px");
		
		timerLabel.setStyleName("timer");
		
		Timer timer = new Timer() {
			@SuppressWarnings("deprecation")
			@Override
			public void run() {
				// TODO Auto-generated method stub
				if (new Date().getHours() > 12) {
					timerLabel.setText(new Date().getHours() + " : " + new Date().getMinutes() + " : " + new Date().getSeconds() + " PM");
				}
				else {
					timerLabel.setText(new Date().getHours() + " : " + new Date().getMinutes() + " : " + new Date().getSeconds() + " AM");
				}
			}
		};
		timer.run();
		timer.scheduleRepeating(1000);
		
		rightBody.add(profilePic);
		rightBody.add(datePicker);
		rightBody.add(timerLabel);

		welcome.setText("Welcome User");
		welcome.setStyleName("pagesHeader");
		
		individualProjects.setText("Individual Projects");
		individualProjects.addItem("Project 1");
		individualProjects.addItem("Project 2");
		
		individualTree.addItem(individualProjects);
		individualTree.setStyleName("treeProjects");
		
		teamProjects.setText("Team Projects");
		teamProjects.addItem("Project 1");
		teamProjects.addItem("Project 2");
		
		teamTree.addItem(teamProjects);
		teamTree.setStyleName("treeProjects");
		
		createIndividual.setText("Individual Project");
		createIndividual.addItem(creteIndividualProjectForm());
		
		createTeam.setText("Team Project");
		createTeam.addItem(createTeamProjectForm());
		
		createProject.setText("Create New Project");
		createProject.addItem(createIndividual);
		createProject.addItem(createTeam);
		
		creteTree.addItem(createProject);
		creteTree.setStyleName("treeProjects");
		
		onlineUsers.setText("Online Developers");
		onlineUsers.addItem("You Only");
		
		onlineTree.addItem(onlineUsers);
		onlineTree.setStyleName("treeProjects");
		
		final Button button = new Button("UML Drawer");
		
		button.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent arg0) {
				History.newItem("UMLDrawer");
			}
		});
		
		leftBody.add(welcome);
		leftBody.add(individualTree);
		leftBody.add(teamTree);
		leftBody.add(creteTree);
		leftBody.add(onlineTree);
		leftBody.add(button);
		leftBody.setStyleName("accountLeftPanel");
		
		body.add(leftBody);
		body.add(rightBody);
		
		wholeBody.add(new HeaderButtons().getHeaderButtons());
		wholeBody.add(body);
		wholeBody.add(new PrivacyBar().getprivacyBar());
		
		wholeBody.setStyleName("border");
		
		mainPanel.add(header);
		mainPanel.add(wholeBody);
		mainPanel.add(footer);
	}

	@Override
	public VerticalPanel getPageContent() {
		setPageContent();
		return mainPanel;
	}
	
	public VerticalPanel creteIndividualProjectForm () {
		VerticalPanel individualProjectForm = new VerticalPanel();
		Label projectName = new Label("Project Name");
		final Image createButton = new Image("images/create.png");
		projectName.setStyleName("createProjectLabels");
		individualProjectNameBox.setStyleName("createProjectTextBox");
		createButton.setStyleName("ProDesUMLButton");
		individualProjectForm.add(projectName);
		individualProjectForm.add(individualProjectNameBox);
		individualProjectForm.add(createButton);
		
		createButton.addMouseOverHandler(new MouseOverHandler() {
			@Override
			public void onMouseOver(MouseOverEvent event) {
				createButton.setUrl("images/create_.png");
			}
		});
		
		createButton.addMouseOutHandler(new MouseOutHandler() {
			@Override
			public void onMouseOut(MouseOutEvent event) {
				createButton.setUrl("images/create.png");
			}
		});
		
		createButton.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				validateCreatingProject();
			}
		});
		
		return individualProjectForm;
	}

	public VerticalPanel createTeamProjectForm () {
		VerticalPanel teamProjectForm = new VerticalPanel();
		Label projectName = new Label("Project Name");
		Label teamNames = new Label("Team Names");
		final Image createButton = new Image("images/create.png");
		projectName.setStyleName("createProjectLabels");
		teamProjectNameBox.setStyleName("createProjectTextBox");
		teamNames.setStyleName("createProjectLabels");
		membersNames.setStyleName("createProjectTextBox");
		createButton.setStyleName("ProDesUMLButton");
		membersNames.setWidth("160px");
		teamProjectForm.add(projectName);
		teamProjectForm.add(teamProjectNameBox);
		teamProjectForm.add(teamNames);
		teamProjectForm.add(membersNames);
		teamProjectForm.add(createButton);
		createButton.addMouseOverHandler(new MouseOverHandler() {
			@Override
			public void onMouseOver(MouseOverEvent event) {
				createButton.setUrl("images/create_.png");
			}
		});
		
		createButton.addMouseOutHandler(new MouseOutHandler() {
			@Override
			public void onMouseOut(MouseOutEvent event) {
				createButton.setUrl("images/create.png");
			}
		});
		
		return teamProjectForm;
	}
	
	public void validateCreatingProject () {
		final DialogBox dialogBox = new DialogBox();
		VerticalPanel dialogBoxPanel = new VerticalPanel();
		Label errorMessage = new Label();
		Button exit = new Button("Okay");
		if (individualProjectNameBox.getText().equals("")) {
			errorMessage.setText("You must enter the project name.");
		}
		else {
			errorMessage.setText("The project '" + individualProjectNameBox.getText() + "' has been added.");
		}
		dialogBoxPanel.add(errorMessage);
		dialogBoxPanel.add(exit);
		dialogBox.setText("Inform Message");
		dialogBox.setWidget(dialogBoxPanel);
		dialogBox.setAnimationEnabled(true);
		dialogBox.center();
		
		exit.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				dialogBox.clear();
				dialogBox.hide();
			}
		});
	}
}
